import { mapState } from 'vuex';
// eslint-disable-next-line no-unused-vars
import { constantRouterMap } from '@/config/router.config.js';

const mixin = {
  props: {
    type: {
      default: 1,
      type: Number
    },
    menus: {
      default: () => {
        return [];
      },
      type: Array
    },
    defaualtKey: {
      default: () => {
        return [];
      },
      type: Array
    },
    menuTree: {
      default: () => {
        return [];
      },
      type: Array
    }
  },
  data() {
    return {
      key: [],
      one: false,
      changeKey: [],
      nodeKey: [],
      tree: [],
      lastKey: '',
      latestOpenKey: '',
      // // 菜单缩进
      // inlineIndent: 12,
      // // 默认不折叠
      collapsed: false,
      // 全部父节点
      rootSubmenuKeys: ['/index'],
      openKeysSelect: [], // 默认展开的节点
      defaultopenKeysSelect: [''],
      // 选中的子菜单项
      defaultSelectedKeys: [this.$route.path],
      autoselectedKeys: [],
      selectedKeysArr: [],

      menu: []
    };
  },
  computed: {
    ...mapState({
      userInfo: state => state.user.userInfo,
      // userPermissions: state => state.user.userPermissions,
      menuLists: state => state.user.menuList

    })
    // menuList() {
    //   // console.log(menuRouterMap);
    //   const menu = constantRouterMap[0].children;
    //   this.deleteHiddenMenu(menu);
    //   return menu;
    // }
  },
  watch: {
    '$route'(newRouter) {
      if (newRouter) {
        this.setSelectMenu(newRouter, true);
      }
    },
    menu: {
      handler(n) {
        if (n) {
          // this.deleteHiddenMenu(n);
          // this.menuList = n;
        }
      },
      immediate: true,
      deep: true
    }
  },
  created() {
    // const menu = JSON.parse(JSON.stringify(constantRouterMap[1].children));
    // this.setSelectMenu(this.$route);
    // // const menu = constantRouterMap[0].children;
    // this.deleteHiddenMenu(menu);

    // const that = this;
    // this.menuList = menu;

    // console.log(menu);

    // this.$set(that, 'menuList', menu);
  },
  mounted() { },

  methods: {
    setSelectMenu(router, bool) {
      this.openKeysSelect = this.$ss.get('openKeysSelect') || [];
    },
    deleteHiddenMenu(data) {
      var newTree = data.filter(e => !e.meta.hidden !== 1 && this.menuLists.includes(e.meta.id));
      newTree.forEach(e => e.children && (e.children = this.deleteHiddenMenu(e.children)));
      return newTree;
    },
    //  控制只打开一个
    onOpenChange(openKeysSelect) {
      // const latestOpenKey = openKeysSelect.find(key => this.openKeysSelect.indexOf(key) === -1);
      // if (this.rootSubmenuKeys.indexOf(latestOpenKey) === -1) {
      //   this.openKeysSelect = openKeysSelect;
      // } else {
      //   this.openKeysSelect = latestOpenKey ? [latestOpenKey] : [];
      // }
      this.openKeysSelect = openKeysSelect;
      this.$ss.set('openKeysSelect', this.openKeysSelect);
    },
    // 点击菜单，路由跳转,注意的是当点击MenuItem才会触发此函数
    menuClick({ item, key, keyPath }) {
      console.log('menuClick');
      this.reset(key);
      this.$store.commit('SET_TASK', item);
      this.$router.push({ key, query: { showTree: 1 } });
    },
    subMenuClick(item) {
      console.log('subMenuClick', item);
      this.$router.push({ path: item.path });
    },
    getIcon(name) {
      return this['icon' + name];
    }
  }
};

export {
  mixin
};
